﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Store_AvailabilityBufabs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (gvBufabs.SelectedValue != null)
        {
            AddValue("+");
        }
        else
        {
            lblMsg.Text = "Моля изберете плат от таблицата с обкови";
        }
    }

    private void AddValue(string operation)
    {
        if (txtValue.Text != String.Empty)
        {
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["KonturConnectionString"].ConnectionString);
            string myInsertQuery = "UPDATE BufabMaterials SET Quantity = ((select quantity from BufabMaterials WHERE BufabMaterialID =" 
                + gvBufabs.SelectedValue.ToString() + ")"+ operation + txtValue.Text + ") WHERE BufabMaterialID = " + gvBufabs.SelectedValue.ToString();
            SqlCommand myCommand = new SqlCommand(myInsertQuery);
            myCommand.Connection = myConnection;
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
            txtValue.Text = String.Empty;
            lblMsg.Text = String.Empty;
            gvBufabs.DataBind();
        }
        else
        {
            lblMsg.Text = "Моля въведете стойност!";
        }
    }
    protected void btnSubstract_Click(object sender, EventArgs e)
    {
        if (gvBufabs.SelectedValue != null)
        {
            AddValue("-");
        }
        else
        {
            lblMsg.Text = "Моля изберете плат от таблицата с обкови";
        }
    }
    protected void gvBufabs_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblMsg.Text = String.Empty;
    }
}
